#include <iostream>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
		struct sockaddr_in serv_addr;
		if( argc < 2 ) {
				return 0;
		}
		int port = atoi(argv[1]);

		int sockfd = socket(AF_INET, SOCK_STREAM, 0);
		if( sockfd < 0 ) {
				printf("socket error\n");
				return 0;
		} else {
				printf("Opened fd %d\n", sockfd);
		}

		bzero((char *) &serv_addr, sizeof(serv_addr));
		serv_addr.sin_family = AF_INET;
		serv_addr.sin_addr.s_addr = INADDR_ANY;
		serv_addr.sin_port = htons(port);
		if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {

				if( errno == EADDRINUSE )
				{
						std::cout<<"the port is not available. already to other process"<<std::endl;
				} 
				else 
				{
						std::cout<<("could not bind to process (%d) %s\n", errno, strerror(errno))<<std::endl;
				}
		}

		//if (close (sockfd) < 0 ) {
		//		printf("did not close fd: %s\n", strerror(errno));
		//		return errno;
		//}

		sleep(1000);
		return 0;


} 
